# -*- coding:utf-8 -*-



# 查询全球ip地理位置和信息
import random

import requests
import time
from catAPI import *


def cx_ip(IP, botID, to_wxID):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'
    }
    html = requests.get(url='http://ip-api.com/json/' + IP + '?lang=zh-CN', headers=headers)
    jsonarr = json.loads(html.content.decode('utf-8'))
    if jsonarr["status"] == "success":
        Allresult = '====查询ip' + IP + '====' + '\n' \
                    '国家：' + jsonarr["country"] + '\n' \
                    '省份：' + jsonarr["regionName"] + '\n' \
                    '城市：' + jsonarr["city"] + '\n' \
                    '纬度：' + str(jsonarr["lat"]) + '\n' \
                    '经度：' + str(jsonarr["lon"]) + '\n' \
                    '时区：' + jsonarr["timezone"] + '\n' \
                    'ISP：' + jsonarr["isp"]
        time.sleep(random.randint(1, 5))
        send_text_msg(botID, to_wxID, Allresult)
    else:
        time.sleep(random.randint(1, 5))
        send_text_msg(botID, to_wxID, jsonarr["message"])
